<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Question extends AdminController {

	function __construct() {
		parent::__construct();
		$this -> load-> model('Question_model');
	}

	function Index() {	
		$this->load->view('admin/questionList');
	}
	function getDatas($params = array(), $isExport = false) {
		$connar = array();
		if (!$isExport) {
			$params = $_POST;
			$connar = $this -> Question_model -> pickPages($connar, $params);
		}

		$connar['orderby'] = "id asc";
		$connar['where']=$this->sqlEscape("db_question.status=?? and (db_question.creator=?? or db_admin.role_id=?? or find_in_set(??,sub_role_id))",array(CommonStatus::UnDeleted[XPHEnum::Value],getSess()['id'],WebRole::Manage[XPHEnum::Value],WebRole::Manage[XPHEnum::Value]));
		$connar['join']=array('db_admin','db_admin.id=db_question.creator');
		$connar['items']='db_question.*,db_admin.username';
		// if(getSess()['role_id']==WebRole::)
		
		if (isset($params['param'])) {
			$connar['where'] .= $this->sqlLikeEscape(" and (db_admin.username like '%??%' ",array($params['param']));
			$connar['where'] .= $this->sqlLikeEscape(" or db_question.title like '%??%') ",array($params['param']));
		}

		if (!$isExport) {
			$data = $this -> Question_model -> get_page_list($connar);
		} else {
			$data["ret"] = $this -> Question_model -> get_list_full($connar);
		}
		foreach ($data['ret'] as $k => $v) {
			$data['ret'][$k]['created']=date('Y-m-d H:i:s',$v['created']);	
			$data['ret'][$k]['img_char']='<img src="'.$v['img'].'" class="question_img" style="width:100px; height:50px;" onclick="imgBigshow(this)">';
			
		}
		if (!$isExport) {
			echo json_encode($data);
		} else {
			return $data["ret"];
		}
	}

	//新增，修改
	function edit($id){
		if($id == 0){
			$data['title'] = "新增问卷";
			$data['detail'] = $this -> Question_model -> get_single(array("id" => $id));
		}else{
			$data['title'] = "编辑问卷";
			$data['detail'] = $this -> Question_model -> get_single(array("id" => $id));
			$data['detail']['img_base64']="data:image/jpeg;base64,".base64_encode(base_url().$data['detail']['img']);
		}
		$this -> load -> view('admin/questionEdit',$data);
	}

	function save(){
		$img=$_POST['question_img'];		
		if(isset($_POST['question_img'])){
			$img=$this->NOTXSS_POST['question_img'];
			if (checkStringIsBase64($img)) {
				$checkRet = uploadImg($img, 'question');
				if (!$checkRet['status']) {
					exit(toRetJson($checkRet));
				}
				$question['img']=$checkRet["v"];
			} else {
				if(!strstr($img, 'upload')){
					exit(retJson('上传的图片格式不正确！', false));
				}				
			}
		}

		$result=$this->Question_model->get_single(array('title'=>$_POST['title'],'status'=>CommonStatus::UnDeleted[XPHEnum::Value]));		
		$question['title']=$_POST['title'];
		if($_POST['id']==0){
			$question['created']=time();
			$question['status']=CommonStatus::UnDeleted[XPHEnum::Value];
			$question['creator']=getSess()['id'];			
			$this->Question_model->add($question);
		}else{
			$question['id']=$_POST['id'];
			$question['updated']=time();
			$this->Question_model->update($question);
		}		
		
		exit(retJson('保存成功！',true));	
	}

	function delete($id){
		$this->Question_model->update(array('id'=>$id,'status'=>CommonStatus::Deleted[XPHEnum::Value]));
		exit(retJson('删除成功',true));
	}
}
